﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;

using XJX.DBCore;

namespace XJX.DataLayer
{
    public sealed class SysMessage : XJX.DBCore.DBObject
    {
        public SysMessage(string newConnectionString) : base(newConnectionString) { }

        //得到某位家庭的所有信件信息
        public DataSet Get_One_Family_Message_From_UID(string UID, string TableName, int PageIndex, int PageSize, out int PageCount, out int TotalCount)
        {
            SqlParameter[]
                parameters = {
								 new SqlParameter("@UID", SqlDbType.UniqueIdentifier),
								 new SqlParameter("@pageindex", SqlDbType.Int),
								 new SqlParameter("@pagesize", SqlDbType.Int)
							 };

            parameters[0].Value = new Guid(UID);
            parameters[1].Value = PageIndex;
            parameters[2].Value = PageSize;

            using (DataSet ds = RunProcedure("Get_One_Family_Message_From_UID", parameters, TableName, out PageCount, out TotalCount))
            {
                return ds;
            }
        }

                //删除一条家庭信件
        public bool Del_One_Family_Message_From_ID(string ID)
        {
            int rowsAffected;

            SqlParameter[]
                parameters = {
								 new SqlParameter("@ID",SqlDbType.Int)
			};

            parameters[0].Value = ID;

            try
            {
                RunProcedure("Del_One_Family_Message_From_ID", parameters, out rowsAffected);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return (rowsAffected == 1);
        }

        //添加站内信息
        public bool Add_New_SysMsg
            (
            string uid,
            string bid,
            string title,
            string msg,
            out string ReturnFieldValue   //返回字段值
            )
        {
            int rowsAffected;//受影响的行数．

            SqlParameter[]
                parameters = {
								 new SqlParameter("@uid",SqlDbType.UniqueIdentifier),
                                 new SqlParameter("@bid",SqlDbType.VarChar,12),
                                 new SqlParameter("@title",SqlDbType.VarChar,200),
                                 new SqlParameter("@msg",SqlDbType.VarChar,4000)
			};

            parameters[0].Value = new Guid(uid);
            parameters[1].Value = bid;
            parameters[2].Value = title;
            parameters[3].Value = msg;

            try
            {
                RunProcedure("Add_New_SysMsg", parameters, out rowsAffected,out ReturnFieldValue);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return (rowsAffected == 1);
        }

        //添加未成功发送的短信消息
        public bool Add_New_NoSuccessShortMsg
            (
            string SysMsgID,
            string TemplateID,
            string ClientIP
            )
        {
            int rowsAffected;//受影响的行数．

            SqlParameter[]
                parameters = {
								 new SqlParameter("@SysMsgID",SqlDbType.Int),
                                 new SqlParameter("@TemplateID",SqlDbType.Int),
                                 new SqlParameter("@ClientIP",SqlDbType.VarChar,15)
			};

            parameters[0].Value = SysMsgID;
            parameters[1].Value = TemplateID;
            parameters[2].Value = ClientIP;

            try
            {
                RunProcedure("Add_New_NoSuccessShortMsg", parameters, out rowsAffected);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return (rowsAffected == 1);
        }


        //Add_New_NoSuccessShortMsg

        //标记一条信息为已读
        public bool Mark_One_SysMsg_Readed(string ID)
        {
            int rowsAffected;

            SqlParameter[]
                parameters = {
								 new SqlParameter("@ID",SqlDbType.Int)
			};

            parameters[0].Value = ID;

            try
            {
                RunProcedure("Mark_One_SysMsg_Readed", parameters, out rowsAffected);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return (rowsAffected == 1);
        }

    }

}
